<?php

require_once dirname(__FILE__).'/../lib/evenementGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/evenementGeneratorHelper.class.php';

/**
 * evenement actions.
 *
 * @package    tfe
 * @subpackage evenement
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class evenementActions extends autoEvenementActions {

    public function executeAutocomplete(sfWebRequest $request) {
        $this->getResponse()->setContentType('application/json');
        $request = Doctrine::getTable('Evenement')->createQuery()
                ->where("libelle LIKE ?","%".$request->getParameter('q')."%")
                ->limit($request->getParameter('limit'))
                ->execute()
                ->getData();

        $evenements = array();
        foreach ( $request as $evenement ) {
            $evenements[$evenement->id] = (string) $evenement .' '. $evenement->getDate();
        }
        return $this->renderText(json_encode($evenements));
    }


    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

//    // vérifier que un seul type d'évènement est rempli
//    if( $form->getValue('id_spectacle') != null && !($form->getValue('id_spectacle') != null ) )
//
//    // définir le type en fonction


        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            $evenement = $form->save();

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $evenement)));

            if ($request->hasParameter('_save_and_add')) {
                $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

                $this->redirect('@evenement_new');
            }
            else {
                $this->getUser()->setFlash('notice', $notice);

                $this->redirect(array('sf_route' => 'evenement_edit', 'sf_subject' => $evenement));
            }
        }
        else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }
}
